<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/import')">excel导入</el-button>
          <el-button type="danger" size="small" @click="doImport">excel导出</el-button>
          <el-button type="primary" size="small" @click="doAdd">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" :index="Nindex" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="照片">
            <template v-slot="scope">
              <img :src="scope.row.staffPhoto" alt="" width="100%">
            </template>
          </el-table-column>
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template v-slot="scope">
              {{ formatEmp(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" />
          <el-table-column label="操作" width="280">
            <template v-slot="scope">
              <el-button type="text" size="small" @click="$router.push('/detail?id='+scope.row.id)">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="doDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            layout="total,sizes,prev, pager, next"
            :total="total"
            :page-sizes="[2,5,10,20]"
            :page-size="size"
            :current-page="page"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
    </div>
    <!-- 弹框 -->
    <el-dialog :visible.sync="showDialog" title="新增员工">
      <empForm @close="hClose" />
    </el-dialog>
  </div>
</template>
<script>
import { getUserInfo, delEmp } from '@/api/employess'
import employees from '@/constant/employees'
import empForm from './empForm.vue'
const hireType = employees.hireType
export default {
  components: { empForm },
  data() {
    return {
      list: [],
      total: 0,
      page: 1,
      size: 20,
      showDialog: false
    }
  },
  created() {
    // 获取员工信息
    this.loadUserInfo()
  },
  methods: {
    // 获取员工信息
    async loadUserInfo() {
      const res = await getUserInfo({ page: this.page, size: this.size })
      this.list = res.data.rows
      this.total = res.data.total
      console.log(res)
    },
    handleCurrentChange(page) {
      this.page = page
      this.loadUserInfo()
    },
    handleSizeChange(size) {
      this.size = size
      this.loadUserInfo()
    },
    // 删除
    async doDel(id) {
      // 如果删除的是最后一页的数据就page--
      if (this.list.length === 1) {
        this.page--
        this.page = Math.max(1, this.page)
      }
      try {
        await delEmp(id)
        this.$message.success('删除成功')
        this.loadUserInfo()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    // 转换聘用形式
    formatEmp(num) {
      const item = hireType.find(it => it.id === num)
      return item ? item.value : '未知'
    },
    // 新增按钮
    doAdd() {
      this.showDialog = true
    },
    hClose() {
      // 添加完后请求最后一页的数据
      this.total++
      this.page = Math.ceil(this.total / this.size)
      this.showDialog = false
      this.loadUserInfo()
    },
    Nindex(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    // 导出
    doImport() {
      import('@/vendor/Export2Excel').then(async excel => {
        // excel表示导入的模块对象
        console.log(excel)
        const res = await getUserInfo()
        const { header, data } = this.transFormData(res.data.rows)
        console.log(res)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 转换格式
    transFormData(rows) {
      const header = Object.keys(rows[0])
      const data = rows.map(item => {
        return header.map(key => {
          return item[key]
        })
      })
      return { data, header }
    }
  }
}
</script>
